package com.codeoss.framework.service.impl;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.dao.TreeMapper;
import com.codeoss.framework.model.TreeModel;
import com.codeoss.framework.service.IBaseTreeService;

public  class BaseTreeService<M extends TreeMapper<T>,T extends TreeModel> extends BaseService<M, T> implements IBaseTreeService<T>{
	@Override
	public Integer getMaxSeqNo(Long parentId) {
		// TODO Auto-generated method stub
		QueryWrapper<T> queryWrapper=new QueryWrapper<T>();
		queryWrapper.select("max(seq_no) as maxseqno").eq("parent_id", parentId);
		List<Map<String,Object>> mapList=listMaps(queryWrapper);
		if(mapList.size()==0) {
			return 0;
		}
		if(mapList.get(0)==null) {
			return 0;
		}
		Object value=mapList.get(0).get("maxseqno");
		return (Integer)value;
	}

	@Override
	public List<T> getDescendTreeNode(String path) {
		// TODO Auto-generated method stub
		QueryWrapper<T> queryWrapper=new QueryWrapper<T>();
		queryWrapper.likeRight("path", path);
		return list(queryWrapper);
	}

	@Override
	public Integer getChildrenCount(Long parentId) {
		// TODO Auto-generated method stub
		QueryWrapper<T> queryWrapper=new QueryWrapper<T>();
		queryWrapper.select("count(1) as count").eq("parent_id", parentId);
		List<Map<String,Object>> mapList=listMaps(queryWrapper);
		if(mapList.size()==0) {
			return 0;
		}
		if(mapList.get(0)==null) {
			return 0;
		}
		Object value=mapList.get(0).get("count");
		if(value instanceof Long) {
			value=Integer.parseInt(String.valueOf(value));
		}
		return (Integer)value;
	}
}
